/*
 * note.c
 *
 *  Created on: Feb 27, 2013
 *      Author: Vincent & Jimmy
 */
#include "support_common.h" /* include peripheral declarations and more */
#include <stdio.h>
#include "note.h"


Note create_note(Pitch p, Duration d)
{
        Note temp;
        temp.p = p;
        temp.d = d;
        return temp;
}

void set_pitch (Note newnote)
{
        switch (newnote.p){
                case Pitch_E4: //329.63 Hz, 3.034 ms
                                        MCF_PWM_PWMPRCLK = 0x0;
                                        MCF_PWM_PWMSCLB = 0xEE;
                                        MCF_PWM_PWMPER7 = 0xFF;
                                        MCF_PWM_PWMDTY7 = 0xCC;
                                        break;
                case Pitch_G4: // 392Hz, 2.551 ms
                                        MCF_PWM_PWMPRCLK = 0x00;
                                        MCF_PWM_PWMSCLB = 0xC8;
                                        MCF_PWM_PWMPER7 = 0xFF;
                                        MCF_PWM_PWMDTY7 = 0xCC;
                                        break;
                case Pitch_C4:
                                        MCF_PWM_PWMPRCLK = 0x01;
                                        MCF_PWM_PWMSCLB = 0x9F;//0F5;
                                        MCF_PWM_PWMPER7 = 0xFF;//0x9C;
                                        MCF_PWM_PWMDTY7 = 0xCC;//0x7D;
                                        break;
                case Pitch_F4:
                                        MCF_PWM_PWMPRCLK = 0x00;
                                        MCF_PWM_PWMSCLB = 0xE1;
                                        MCF_PWM_PWMPER7 = 0xE6;
                                        MCF_PWM_PWMDTY7 = 0xB8;
                                        break;
                case Pitch_D4:
                                        MCF_PWM_PWMPRCLK = 0x00;
                                        MCF_PWM_PWMSCLB = 0xF9;
                                        MCF_PWM_PWMPER7 = 0xE6;
                                        MCF_PWM_PWMDTY7 = 0xB8;
                                        break;
                                       
                case Pitch_00:
                                                                MCF_PWM_PWMPER7 = 0x01;
                                            MCF_PWM_PWMDTY7 = 0x01;
                                            break;
                default:
                        break;
                       
        }
}

void set_duration (Note newnote)
{
        switch (newnote.d){
                case Duration_Quarter: //Count = 1221
                                                                MCF_PIT0_PMR  =0x04C5;
                                        break;
                case Duration_Half: //Count = 2441
                                                                MCF_PIT0_PMR  =0x0989;
                                        break;
                case Duration_Whole: // Count = 4883
                                        MCF_PIT0_PMR  = 0x1313;
                                        break;
                default:
                        break;
                       
        }
}

void set_delay (int bpm)
{
        switch (bpm)
        {
                case 60 :
                        MCF_PIT0_PMR  = 0x1313; //4663
                        BPM = 60;
                        break;
                case 70 :
                        MCF_PIT0_PMR  = 0x1059; //4185
                        BPM =70;
                        break;
                case 80 :
                        MCF_PIT0_PMR  =0x0E4E; //3662
                        BPM =80;
                        break;
                case 90 :
                        MCF_PIT0_PMR  = 0x0CB7; //3255
                        BPM =90;
                        break;
                case 100 :
                        MCF_PIT0_PMR  = 0x0B72; //2930
                        BPM= 100;
                        break;
                case 110 :
                        MCF_PIT0_PMR  = 0x0A67; //2663
                        BPM =110;
                        break;
                case 120 :
                        MCF_PIT0_PMR  = 0x0989; //2441
                        BPM =120;
                        break;
                default:
                        break;
        }
                       
       
}
